package com.xumt.hwHMSdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;

import com.huawei.hms.videokit.player.WisePlayer;
import com.huawei.hms.videokit.player.WisePlayerFactory;

public class VideoPlayActivity extends AppCompatActivity {

    public static final String VIDEO_URL = "video_play_url";
    public static final String VIDEO_NAME = "video_play_name";

    public static final int VIDEO_PLAYING = 1;
    public static final int VIDEO_PAUSING = 0;
    private videoPlayHandler handler = new videoPlayHandler();
    private int isAuto;

    private WisePlayerFactory factory = null;

    private SurfaceView video_play_view;
    private TextView video_play_name;
    private ImageButton video_play_pp;
    private Button video_play_back;
    private TextView video_play_vtime;
    private TextView video_play_ntime;
    private SeekBar video_play_seekbar;
    private int play_statue;
    private int duration;
    private WisePlayer player=null;
    private VideoKitApplication VK;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_play);

        video_play_view = findViewById(R.id.video_play_view);

        video_play_pp = findViewById(R.id.video_play_pp);
        video_play_ntime = findViewById(R.id.video_play_ntime);
        video_play_vtime = findViewById(R.id.video_play_vtime);
        video_play_seekbar = findViewById(R.id.video_play_seekbar);

        play_statue = 1;

        init_Button();

        Intent intent = getIntent();



        player.setPlayUrl("https://videoplay-mos-dra.dbankcdn.com/P_VT/video_injection/92/v3/C072F990370950198572111872/MP4Mix_H.264_1920x1080_6000_HEAAC1_PVC_NoCut.mp4");
        
        player=VK.getWisePlayerFactory().createWisePlayer();;


        SurfaceHolder surfaceHolder = video_play_view.getHolder();
        surfaceHolder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder holder) {
                player.setView(video_play_view);
            }

            @Override
            public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder holder) {

            }
        });
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        player.ready();
        player.setReadyListener(new WisePlayer.ReadyListener() {
            @Override
            public void onReady(WisePlayer wisePlayer) {
                player.start();
                video_play_pp.setImageResource(R.drawable.video_pause);
                video_play_view.setVisibility(View.VISIBLE);
                init_Mes();
                handler.sendEmptyMessage(player.isPlaying()?1:0);
                isAuto = 1;
            }
        });
        player.setSeekEndListener(new WisePlayer.SeekEndListener() {
            @Override
            public void onSeekEnd(WisePlayer wisePlayer) {
                Log.d("video_seek_re",Integer.toString(player.getCurrentTime()));
                player.start();
            }
        });
        video_play_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void init_Mes() {

        duration = player.getDuration();
        int sec = duration/1000;
        int min = sec/60;
        String vtime = "";
        vtime += min + ":" + sec;
        video_play_vtime.setText(vtime);
    }

    private class videoPlayHandler extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case VIDEO_PLAYING:
                    int nowms = player.getCurrentTime();
                    int nowsec = nowms/1000;
                    int nowmin = nowsec/60;
                    String ntime = "";
                    ntime += nowmin + ":" + nowsec;
                    video_play_ntime.setText(ntime);
                    sendEmptyMessageDelayed(player.isPlaying()?1:0,1000);
                    Double ms = Double.valueOf(nowms);
                    Double percent = (ms/duration)*100;
                    video_play_seekbar.setProgress(percent.intValue());
                    isAuto = 1;
                    break;
                case VIDEO_PAUSING:
                    sendEmptyMessageDelayed(player.isPlaying()?1:0,1000);
                    break;
                default:
                    break;
            }
        }
    }

    private void init_Button() {
        video_play_pp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(play_statue==0) {
                    play_statue = 1;
                    player.start();
                    video_play_pp.setImageResource(R.drawable.video_pause);
                }
                else{
                    play_statue = 0;
                    player.suspend();
                    video_play_pp.setImageResource(R.drawable.video_play);
                }
            }
        });
        video_play_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Log.d("video_seek_auto",Integer.toString(progress));
                if(isAuto == 1){
                    return;
                }
                Double percent = (progress/100.0);
                Double nowSec = (percent*duration);
                int nowTime = nowSec.intValue();
                Log.d("video_seek",Integer.toString(nowTime));
                player.seek(nowTime);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                player.pause();
                isAuto = 0;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        player.release();
    }
}